package com.insurance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.domain.ResponseResult;
import com.insurance.domain.entity.Category;
import com.insurance.domain.entity.Customer;
import com.insurance.domain.enums.AppHttpCodeEnum;
import com.insurance.domain.vo.CategoryPageVo;
import com.insurance.domain.vo.PageVo;
import com.insurance.mapper.CategoryMapper;
import com.insurance.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2023-04-22 13:06:04
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public ResponseResult listByPage(CategoryPageVo categoryPageVo) {
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(categoryPageVo.getCategoryName()),Category::getCategoryName,categoryPageVo.getCategoryName());
        Page<Category> page = new Page<>(categoryPageVo.getPageNo(), categoryPageVo.getPageSize());
        page(page,queryWrapper);
        List<Category> categoryList = page.getRecords();
        PageVo pageVo = new PageVo(categoryList, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addCategory(Category category) {
        if(save(category)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ADD_FAILED);
    }

    @Override
    public ResponseResult updateCategory(Category category) {
        if(updateById(category)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }
}
